package com.example.financeapi.controller;


import com.example.financeapi.config.annotation.PassToken;
import com.example.financeapi.config.db.ExecuteSql;
import com.example.financeapi.config.db.JdbcDruidUtils;
import com.example.financeapi.config.db.SqlDruidParsers;
import com.example.financeapi.enums.ResultEnum;
import com.example.financeapi.entity.vo.ResultVOUtil;
import com.example.financeapi.entity.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 操作数据源及执行sql
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("sys/dbSource")
public class DbSourceController {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private StringEncryptor stringEncryptor;

    /**
     * 执行器
     * @param sql
     * @return
     */
    @PassToken
    @RequestMapping("/sqlExecute/executeSql")
    public ResultVo executeSql(ExecuteSql sql) {
        List<Map<String, Object>> dataList = new ArrayList<>();
        try {
            if(StringUtils.isBlank(sql.getCode())){
                return ResultVOUtil.error(ResultEnum.PARAM_ERROR.getMessage());
            }
            Map<String, Object> sqlParser = SqlDruidParsers.sqlParser(sql.getDataType(), sql.getSqlText());
            String executeType =sqlParser.get("executeType")+"";
            if (StringUtils.isBlank(executeType)) {
                throw new NullPointerException("SQL解析异常");
            }
            List<String> executeSqlList = (List<String>) sqlParser.get("executeSql");
            if ("SELECT".equals(executeType)) {
                for (String executeSql : executeSqlList) {
                    Map<String, Object> resultData = JdbcDruidUtils.findMoreResult(dataSource, executeSql, new ArrayList<>());
                    dataList.add(resultData);
                }
            } else {
                if ("DELETE".equals(executeType)){
                    String password = stringEncryptor.decrypt("74vKz6ZTYj5YXq1R1V0JU0XPsbLxXEPGuBcCdIEA36+5aDW6EsN10HNGifW8L7Ts");
                    if(!sql.getCode().equals(password)){
                        return ResultVOUtil.error("密钥标识不正确！暂无权限！");
                    }
                }
                for (String executeSql : executeSqlList) {
                    Map<String, Object> resultData = JdbcDruidUtils.updateByPreparedStatement(dataSource, executeSql, new ArrayList<>());
                    dataList.add(resultData);
                }
            }
        } catch (Exception e) {
            log.error("【接口：executeSql】后台异常，e={}" + e.getMessage(), e);
            return ResultVOUtil.error(ResultEnum.PROGRAM_EXCEPTION.getMessage() + e.getMessage(), e);
        }
        return ResultVOUtil.success(dataList);
    }

}
